import { Component, Input, OnChanges, SimpleChanges, OnInit } from '@angular/core';
@Component({
  selector: 'app-countdown',
  template: '<font>{{text}}</font>',
  styles: ['font{color:#333;}']
})
export class CountdownComponent implements OnChanges, OnInit {
  @Input()
  second: number;
  text: string;
  t: any;
  constructor() {}
  ngOnInit() {
  }
  ngOnChanges(changes: SimpleChanges) {
    if (changes['second'].currentValue) {
      this.text = this.getCountdownText(this.second);
      clearInterval(this.t);
      this.timing();
    }
  }
  /**
   * timing 计时
   */
  public timing() {
    this.t = setInterval(() => {
      if (this.second > 0) {
        this.second--;
        this.text = this.getCountdownText(this.second);
      } else {
        clearInterval(this.t);
      }
    }, 1000);
  }
  /**
   * getCountdownText 获取倒计时文字
   */
  public getCountdownText(second) {
    const h = Math.floor(second / 3600);
    const m = Math.floor((second % 3600) / 60);
    const s = Math.floor(second % 60);
    return h + '时 ' + m + '分 ' + s + '秒';
  }
}
